package edu.syr.iis.web.model;


import java.util.Set;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@Entity
public class User {
	
	public static final int ADMIN = 1;
	public static final int ANNOTATOR = 2;
	
	int userId;
	
	Set<AnnotationSession> annotationSessions;
	Set<Assignment> assignments;
	
	String userName;
	String firstName;
	String lastName;
	String password;
	
	int role;		
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	@OneToMany(mappedBy="user", targetEntity=AnnotationSession.class, cascade=CascadeType.PERSIST)
	public Set<AnnotationSession> getAnnotationSessions() {
		return annotationSessions;
	}
	
	public void setAnnotationSessions(Set<AnnotationSession> annotationSessions) {
		this.annotationSessions = annotationSessions;
	}

	@OneToMany(mappedBy="user", targetEntity=Assignment.class)
	public Set<Assignment> getAssignments() {
		return assignments;
	}

	public void setAssignments(Set<Assignment> assignments) {
		this.assignments = assignments;
	}

	@Transient
	public Set<Conversation> getAssignedConversations() {
		HashSet<Conversation> conversations = new HashSet<Conversation>();
		
		for (Assignment assignment: getAssignments())
			conversations.add(assignment.getConversation());

		return conversations; 
	}

	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	public int getRole() {
		return role;
	}
	public void setRole(int role) {
		this.role = role;
	}

	public String toLogString()
	{
		return "userId:                        " + userId +
		       "num of assignments:            " + getAssignments().size() +
		       "num of assigned conversations: " + getAssignedConversations().size() +
			   "num of annotation sessions:    " + annotationSessions.size();		
	}
	@Override
	public String toString() {
		return userName;
	}				
}
